#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>

#define N 32
#define M 256

#define  LED sizeof(MSG)

enum
{
	L=0x1,  //登录
	C,  //聊天
};

typedef struct
{
	
	int type;
	char name[N];
	char text[M];
}MSG;

typedef struct node
{
	struct sockaddr_in addr;
	struct node *next;
}linknode, *linklist;

linklist client_list_create()
{
	linklist head = (linklist)malloc(sizeof(linknode));
	if(head == NULL)
		return NULL;
	else
		return head;
}

void do_login(int sockfd,struct sockaddr_in client addr,linklist head,MSG msg)
{
	linklist p,q;
	p = head->next;
	sprintf(msg.text,"%s login.....",msg.name);
	while(p)
	{
		sendto(sockfd,&msg,LEN,0,(struct sockaddr *)&p->addr,sizeof(struct sockaddr));
		p = p->next;
	}
	q = (linklist)malloc(sizeof(linknode));
	q->addr = clientaddr;
	q->next = head->next;
	head->next = q;
	puts(msg.text);
}

/*
   1.把聊天信息发给其它用户
   2.发给自己一份
   */
void do_chat(int sockfd,struct sockaddr_in clientaddr,linklist head,MSG msg)
{
	linklist p;
	char buf[M] = {0};
	p = head->next;
	sprintf(buf,"%s said %s",msg.name,msg.text);
	strcpy(msg.text,buf);
	while(p)
	{
		if(memcmp(&p->addr, &clientaddr, sizeof(clientaddr)) != 0)
		{
			sendto(sockfd,&msg,LEN,0,(struct sockaddr *) &p->addr,sizeof(struct sockaddr));
		}
		p = p->next;
	}
	puts(msg.text);
}


int main(int argc,const char *argv[])
{
	int sockfd;
	struct sockaddr_in serveraddr, clientaddrr;
	MSG  msg;
	pid_t pid;
	socklen_t len = sizeof(struct sockaddr);
	linklist head;
	if(argc != 3)
	{
		printf("user:%s ip port",argv[0]);
		return -1;
	}
	//创建socket
		sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sockfd < 0)
	{
		perror("socket err");
		exit(-1);
	}

//绑定服务器地址
bzero(&serveraddr, len);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(argv[2]);
serveraddr.sin_addr.s_addr = iner_addr(argv[1]);
if(bind(sockfd, (struct sockaddr *)&serveraddr, len) < 0)
	{
		perror("bind err");
		exit(-1);
	}

	//fork--子进程发送消息，父进程接收消息并处理
	pid = fork();
	if(pid < 0)
	{
		perror("fork errno");
		exit(-1);
	}else if(pid == 0)//zijincheng
	{
		while(1)
		{
			strcpy(msg.name,"server");
			gets(msg.text);
			msg.type = C;
			sendto(sockfd,&msg,LEN,0,(struct sockaddr*)&serveraddr,len);

		}
	}
	else//fujincheng
		//	创建客户端链表 
	{
		head = client_list_create();
		if(NULL == head)
		{
			printf("link create errno\n");
			exit(-1);
		}
		while(1)//让程序不死
		{
			recvfrom(sockfd,&msg,LEN,0,(struct sockaddr *) &clientaddr,&len);
			switch(msg.type)
			{
				case L:
					do_login(sockfd,clientaddr,head,msg);
					break;
				case C:
					do_chat(sockfd,clientaddr,head,msg);
					break;
				defaulit:
					puts("error mag type");
				
			}
		}
	}
}
	

